package com.mediacloud.app.newsmodule.fragment.baoliao.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.assembly.views.CornerBlurView;
import com.mediacloud.app.model.news.BaoLiaoMeta;
import com.mediacloud.app.nav2.utils.DateUtils;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.baoliao.OnItemClickListener;
import com.mediacloud.app.newsmodule.fragment.baoliao.XPlayerVideoListPlayerUtil;
import com.mediacloud.app.newsmodule.fragment.baoliao.model.detail.ImageGridItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yoake on 2018/7/25.
 */

public class BaoLiaoDetailAdapter extends RecyclerView.Adapter<BaoLiaoDetailAdapter.BaoLiaoHolder> implements View.OnClickListener {
    private List<ImageGridItem> list;
    private Context context;
    private OnItemClickListener clickListener;
    private int spancount;
    private BaoLiaoMeta meta;
    public XPlayerVideoListPlayerUtil videoListPlayerUtil;

    public BaoLiaoDetailAdapter(Context context, OnItemClickListener clickListener, View recyclerView) {
        this.context = context;
        this.clickListener = clickListener;
        this.list = new ArrayList<>();
        videoListPlayerUtil = new XPlayerVideoListPlayerUtil(context, recyclerView);
    }


    public void setGridList(List<ImageGridItem> list, BaoLiaoMeta meta) {
        this.meta = meta;
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public ImageGridItem getItem(int position) {
        return list.get(position);
    }

    public List<ImageGridItem> getData() {
        return list;
    }

    @NonNull
    @Override
    public BaoLiaoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new BaoLiaoHolder(LayoutInflater.from(context).inflate(R.layout.baoliao_media_detail_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final @NonNull BaoLiaoHolder holder, int position) {
        ImageGridItem item = list.get(position);
        holder.delete.setVisibility(View.GONE);
       /* int width = context.getResources().getDisplayMetrics().widthPixels;

        int height = (width - context.getResources().getDimensionPixelOffset(R.dimen.dimen60)) / spancount;
        lp.height = height;
        lp.width = height;*/
        /*if (holder.itemView.getParent() == null) {
            holder.itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    if (holder.itemView.getParent() != null) {
                        resize((View) holder.itemView.getParent(), holder.imageLayout);
                        holder.itemView.getViewTreeObserver().removeOnPreDrawListener(this);
                    }
                    return false;
                }
            });
        } else {
            resize((View) holder.itemView.getParent(), holder.imageLayout);
        }*/
//        ViewGroup.LayoutParams lp = holder.imageLayout.getLayoutParams();
        //holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if (item.isVideo()) {
            holder.videoTag.setVisibility(View.VISIBLE);
            holder.tvVideoTime.setVisibility(View.VISIBLE);
            holder.videoContainer.setVisibility(View.VISIBLE);
            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) holder.videoTag.getLayoutParams();
            if (getItemCount() == 1) {
                holder.tvVideoTime.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
               /* layoutParams.removeRule(RelativeLayout.CENTER_IN_PARENT);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);*/
        /*        layoutParams.startToStart=layoutParams.PARENT_ID;
                layoutParams.topToTop=layoutParams.UNSET;
                layoutParams.endToEnd=layoutParams.UNSET;
                layoutParams.bottomToBottom=layoutParams.PARENT_ID;
                int margin = context.getResources().getDimensionPixelOffset(R.dimen.dimen10);
                layoutParams.setMargins(margin, 0, 0, margin);*/
                //20200518新增一个需求，当只有一个视频的时候，直接添加一个视频，点击直接播放
                setVideoPlayer(holder);
            } else {
                holder.videoContainer.setVisibility(View.GONE);
                holder.videoTag.setVisibility(View.GONE);
                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_image_model_video);
                holder.tvVideoTime.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                holder.tvVideoTime.setText(DateUtils.millsecondToMinuteSecond(Integer.parseInt(item.getDuration()) * 1000));
                /* *//* layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);*//*
                layoutParams.startToStart=layoutParams.PARENT_ID;
                layoutParams.topToTop=layoutParams.PARENT_ID;
                layoutParams.endToEnd=layoutParams.PARENT_ID;
                layoutParams.bottomToBottom=layoutParams.PARENT_ID;
                layoutParams.setMargins(0, 0, 0, 0);*/
            }
            // holder.videoTag.setLayoutParams(layoutParams);
            if (meta != null) {
                if (meta.getTipoffinfoList() != null && meta.getTipoffinfoList().size() > 0) {
                    if (meta.getTipoffinfoList().get(0).getLiveAddress() != null && meta.getTipoffinfoList().get(0).getLiveAddress().size() > 0) {
                        if (meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats() != null && meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().size() > 0) {
                            if(meta.getShortTitleStyle()!=null && meta.getShortTitleStyle().size()>0){
                                if(!TextUtils.isEmpty(meta.getShortTitleStyle().get(0).getImgpath())){
                                    holder.image.load(meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().get(0).getImage());
                                }
                            }
                        }
                    }
                }
            }
        } else {
            holder.videoContainer.setVisibility(View.GONE);
            holder.videoTag.setVisibility(View.GONE);
            holder.tvVideoTime.setVisibility(View.GONE);
            holder.image.load(item.getPath());
        }
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.default_loading);

        //  Glide.with(context).load(item.getPath()).into(holder.image).onLoadFailed(drawable);
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
    }


    private void setVideoPlayer(BaoLiaoHolder holder) {
        holder.videoContainer.setVisibility(View.VISIBLE);
        holder.videoTag.setVisibility(View.VISIBLE);
        //  holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        holder.image.setOnClickListener(v -> {
/*            holder.videoTag.setVisibility(View.GONE);
            holder.image.setVisibility(View.GONE);*/
            if (videoListPlayerUtil == null)
                return;
            videoListPlayerUtil.setPlayerContainer(holder.itemView, holder.videoContainer, holder.image);
            if (meta != null) {
                if (meta.getTipoffinfoList() != null && meta.getTipoffinfoList().size() > 0) {
                    if (meta.getTipoffinfoList().get(0).getLiveAddress() != null && meta.getTipoffinfoList().get(0).getLiveAddress().size() > 0) {
                        if (meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats() != null && meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().size() > 0) {
                            if(meta.getShortTitleStyle()!=null && meta.getShortTitleStyle().size()>0){
                                if(!TextUtils.isEmpty(meta.getShortTitleStyle().get(0).getImgpath())){
                                    videoListPlayerUtil.setVideoData(meta.getShortTitleStyle().get(0).getImgpath(), meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().get(0).getUrl());
                                }
                            }
                        }
                    }
                }
            }
        });
        holder.videoTag.setOnClickListener(v -> {
          /*  holder.videoTag.setVisibility(View.GONE);
            holder.image.setVisibility(View.GONE);*/
            if (videoListPlayerUtil == null)
                return;
            videoListPlayerUtil.setPlayerContainer(holder.itemView, holder.videoContainer, holder.image);
            if (meta != null) {
                if (meta.getTipoffinfoList() != null && meta.getTipoffinfoList().size() > 0) {
                    if (meta.getTipoffinfoList().get(0).getLiveAddress() != null && meta.getTipoffinfoList().get(0).getLiveAddress().size() > 0) {
                        if (meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats() != null && meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().size() > 0) {
                            if(meta.getShortTitleStyle()!=null && meta.getShortTitleStyle().size()>0){
                                if(!TextUtils.isEmpty(meta.getShortTitleStyle().get(0).getImgpath())){
                                    videoListPlayerUtil.setVideoData(meta.getShortTitleStyle().get(0).getImgpath(), meta.getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().get(0).getUrl());
                                }
                            }
                        }
                    }

                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        clickListener.onClick(position);
    }

    public void setSpancount(int spancount) {
        this.spancount = spancount;
    }


    class BaoLiaoHolder extends RecyclerView.ViewHolder {

        CornerBlurView image;
        ImageView videoTag, delete;
        //        RelativeLayout imageLayout;
//        VideoPlayer videoPlayer;
        RelativeLayout videoContainer;
        TextView tvVideoTime;

        public BaoLiaoHolder(View itemView) {
            super(itemView);
//            imageLayout = itemView.findViewById(R.id.imageLayout);
            image = itemView.findViewById(R.id.image);
            videoTag = itemView.findViewById(R.id.videoTag);
            tvVideoTime = itemView.findViewById(R.id.tv_videotime);
            delete = itemView.findViewById(R.id.delete);
//            videoPlayer = itemView.findViewById(R.id.videoPlayer);
            videoContainer = itemView.findViewById(R.id.container);
        }
    }

    public void destroy() {
        if (videoListPlayerUtil != null) {
            videoListPlayerUtil.onDestroy();
        }
    }
}